package com.app.service.impl;

import com.app.enums.BannerType;
import com.app.mapper.BannerMapper;
import com.app.model.BannerModel;
import com.app.service.BannerService;
import com.app.vo.BannerVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 16693103420
 * @since 2024-01-26
 */
@Service
public class BannerServiceImpl extends ServiceImpl<BannerMapper, BannerModel> implements BannerService {

    @Autowired
    private BannerMapper bannerMapper;

    @Override
    public BannerVo getBannerList(Integer page, Integer limit, Integer type, Integer classification) {
        Page<BannerModel> page1 = new Page<>(page, limit);
        QueryWrapper<BannerModel> qw = new QueryWrapper<>();

        if (type != null) {
            qw.eq("type", type);
        }
        if (classification != null) {
            qw.eq("classification", classification);
        }

        qw.orderByAsc("sort").orderByDesc("create_time");
        Page<BannerModel> bannerPage = bannerMapper.selectPage(page1, qw);
        List<BannerModel> bannerList = bannerPage.getRecords();
        for (BannerModel bannerModel : bannerList) {
            if (bannerModel.getType() != null) {
                bannerModel.setTypeText(BannerType.getTypeText(bannerModel.getType()));
            }
            if (bannerModel.getClassification() != null) {
                if (bannerModel.getClassification() == 1) {
                    bannerModel.setCode("free");
                    bannerModel.setClassificationText("免费");
                }
                if (bannerModel.getClassification() == 5) {
                    bannerModel.setCode("consume");
                    bannerModel.setClassificationText("消费多少送多少");
                }
            }
        }
        long count = bannerPage.getTotal();
        BannerVo bannerVo = new BannerVo();
        bannerVo.setList(bannerList);
        bannerVo.setCount((int) count);
        return bannerVo;
    }
}
